package com.kerbores.jfinal.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.core.ActionInvocation;

/**
 * @author 王贵源
 *
 *         create at 2014年9月23日 下午1:20:47
 */
public class SessionCheckInterceptor implements Interceptor {
	/**
	 * 忽略检查的path
	 */
	private String[] ignorePath;
	/**
	 * 检查失败跳转的path
	 */
	private String path;

	public SessionCheckInterceptor(String[] ignorePath, String path) {
		this.ignorePath = ignorePath;
		this.path = path;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jfinal.aop.Interceptor#intercept(com.jfinal.core.ActionInvocation)
	 */
	@Override
	public void intercept(ActionInvocation ai) {
		// 处理session检查的业务
		if (!contain(ai.getControllerKey(), ignorePath) && ai.getController().getSession().getAttribute("user") == null) {
			ai.getController().redirect(path);
		} else {
			ai.invoke();
		}
	}

	/**
	 * @param controllerKey
	 * @param ignorrePath2
	 * @return
	 */
	private boolean contain(String controllerKey, String[] ignorePath2) {
		for (String path : ignorePath2) {
			if (controllerKey.equals(path)) {
				return true;
			}
		}
		return false;
	}
}
